<?php
namespace app\overlord\model;

use app\BaseModel;
use think\facade\Db;

class RefundOrder extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_overlord_refund_order';


    protected $append = [

        'user_name'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 11:54
     * @功能说明:操作人名称
     */
    public function getUserNameAttr($value,$data){

        if(!empty($data['user_id'])){

            return lbUserName($data['user_id']);
        }

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:内容添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();


        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->alias('a')
                ->join('longbing_card_overlord_atv b','a.atv_id = b.id')
                ->where($dis)
                ->field('a.*,b.title as atv_title,b.num')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['user_name'] = lbUserName($v['user_id']);

            }
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }

    /**
     * @author chenniang
     * @DataTime: 2020-11-26 17:38
     * @功能说明:获取门店总充值金额
     */

    public function getStoreFee($store_id){

        $dis = [

            'store_id' => $store_id,

            'status'   => 2
        ];

        $fee = $this->where($dis)->sum('refund_price');

        return round($fee,2);


    }

    /**
     * @author chenniang
     * @DataTime: 2020-11-30 11:44
     * @功能说明:后台订单列表
     */
    public function adminOrderList($store_id,$page=10,$title=''){

        $dis[] = ['a.store_id','=',$store_id];

        if(!empty($title)){

            $dis[] = ['b.title','like','%'.$title.'%'];
        }

        $data = $this->alias('a')
            ->join('longbing_card_overlord_atv b','a.atv_id =  b.id','left')
            ->where($dis)
            ->field('b.title,b.num,b.cash,a.status as pay_type,a.refund_price as total_cash,a.create_time,a.id')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();


        return $data;

    }

}